package day3.jdbc.dao.refactor;

import java.sql.ResultSet;
import java.sql.SQLException;

import day1.domain.User;

public class UserDaoImpl1 extends AbstractDao {
	
	public User findUserById(int id){
		String sql="select id,name,money from user where id=?";
		Object[] args=new Object[]{id};
		User user=(User) super.find(sql, args);
		return user;
	}
	
	public int  delete(User user) {
		String sql = "delete from user where id=?";
		Object[] args = new Object[] { user.getId() };
		return super.update(sql, args);
	}
	
	public String findUserNameById(int id){
		String sql="select id,name,money from user where id=?";
		Object[] args=new Object[]{id};
		User user=(User) super.find(sql, args);
		return user.getName();
	}

	@Override
	protected Object rowMapper(ResultSet rs) {
		// TODO Auto-generated method stub
		User user=null;
		if(rs!=null){
			try {
				user=new User();
				String name=rs.getString("name");
				int id=rs.getInt("id");
				float money=rs.getFloat("money");
				user.setName(name);
				user.setId(id);
				user.setMoney(money);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return user;
	}

}
